package com.tuanzi.loan.business.repository.member.impl;

import com.tuanzi.loan.business.repository.member.custom.LoanMemberOrderCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.member.LoanMemberOrderRequest;
import com.tuanzi.loan.vo.response.member.LoanMemberOrderResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanMemberOrder.LOAN_MEMBER_ORDER;

/**
 * @Author: chenweijun
 * @Date: 2019/3/29 17:51
 * @Description:
 */
@SuppressWarnings({ "rawtypes" })
@Repository
@Slf4j
public class LoanMemberOrderRepositoryImpl extends JOOQBaseRepository implements LoanMemberOrderCustomRepository{

    @Override
    protected TableImpl getTable() {
        return LOAN_MEMBER_ORDER;
    }

    @Override
    public Page<LoanMemberOrderResponse> list(LoanMemberOrderRequest searchRequest, Pageable pageable) {

        fields(LOAN_MEMBER_ORDER.fields());
        fields(COMM_PRODUCT_PRDID.PRDID, COMM_PRODUCT_PRDID.PARENT_ID);

        leftJoin(COMM_PRODUCT_PRDID, LOAN_MEMBER_ORDER.PRODUCT_ID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)));

        between(LOAN_MEMBER_ORDER.CREATE_TIME, searchRequest.getStartDate(),
                searchRequest.getEndDate() == null ? searchRequest.getEndDate() : DateUtils.getDateEndTime(searchRequest.getEndDate()));

        eq(LOAN_MEMBER_ORDER.PRODUCT_ID, searchRequest.getProductId());
        eq(LOAN_MEMBER_ORDER.CHANNEL_CODE, searchRequest.getChannelCode());
        eq(LOAN_MEMBER_ORDER.PLATFORM, searchRequest.getPlatform());
        eq(LOAN_MEMBER_ORDER.PAY_TYPE, searchRequest.getPayType());
        eq(LOAN_MEMBER_ORDER.NEW_USER, searchRequest.getUserType());
        eq(LOAN_MEMBER_ORDER.STATUS, searchRequest.getStatus());
        eq(LOAN_MEMBER_ORDER.MOBNUM, searchRequest.getMobnum());
        eq(LOAN_MEMBER_ORDER.PAGE_FROM, searchRequest.getPageFrom());
        eq(COMM_PRODUCT_PRDID.PARENT_ID, searchRequest.getParentId());
        orderBy(LOAN_MEMBER_ORDER.CREATE_TIME, Sort.Direction.DESC);


        return search(pageable, record -> {
            LoanMemberOrderResponse response = new LoanMemberOrderResponse();
            response.setCreateTime(record.get(LOAN_MEMBER_ORDER.CREATE_TIME));
            response.setPayTime(record.get(LOAN_MEMBER_ORDER.PAY_TIME));
            response.setProductId(record.get(LOAN_MEMBER_ORDER.PRODUCT_ID));
            response.setChannelCode(record.get(LOAN_MEMBER_ORDER.CHANNEL_CODE));
            response.setPlatform(record.get(LOAN_MEMBER_ORDER.PLATFORM));
            response.setOrderId(record.get(LOAN_MEMBER_ORDER.ORDER_ID));
            response.setMobnum(record.get(LOAN_MEMBER_ORDER.MOBNUM));
            response.setStatus(record.get(LOAN_MEMBER_ORDER.STATUS));
            response.setAmount(record.get(LOAN_MEMBER_ORDER.AMOUNT));
            response.setPageFrom(record.get(LOAN_MEMBER_ORDER.PAGE_FROM));
            response.setParentId(record.get(COMM_PRODUCT_PRDID.PARENT_ID));

            return response;
        });
    }


}
